package com.open.capacity.points.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.PointsRule;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.points.dto.PointsRuleDto;
import com.open.capacity.points.service.PointsRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("pointsRule")
@Api(tags = "POINTS-RULE API")
public class PointsRuleController {

    @Resource
    private PointsRuleService pointsRuleService;

    @GetMapping("/getPointsRule")
    @ApiOperation(value = "根据条件，返回积分规则")
    @LogAnnotation(module = "points-center")
    public PageResult<PointsRuleDto> getPointsRule(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            return pointsRuleService.findRulesByCondition(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "添加或更新积分规则")
    @LogAnnotation(module = "points-center")
    public Result saveOrUpdate(@RequestBody PointsRule pointsRule) throws ControllerException {
        try {
            return pointsRuleService.saveOrUpdated(pointsRule);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @DeleteMapping("/{poruid}")
    public Result deleteRointsRuleById(@PathVariable String poruid) throws ControllerException {
        try {
            return pointsRuleService.delete(poruid);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

}
